<template>
	<view class="settings-page">

		<!-- 头像区域 -->
		<view class="profile-section">
			<view class="avatar-container">
				<view class="avatar-placeholder"></view>
			</view>
			<text class="username">AI摇人</text>
			<text class="version">版本号: 1.0.0</text>
			<button class="check-update-btn" @click="checkUpdate">检查更新</button>
		</view>

		<!-- 设置列表 -->
		<view class="settings-list">
			<!-- 地点自动签到开关 -->
			<view class="setting-item">
				<view class="item-label">地点自动签到</view>
				<view class="item-value">
					<switch :checked="autoSignIn" class="toggle-switch" @change="onAutoSignInChange"></switch>
				</view>
			</view>

			<!-- 修改提现密码 -->
			<view class="setting-item" @click="openModifyWithdrawPassword">
				<view class="item-label">修改提现密码</view>
				<view class="item-value">
					<uni-icons type="arrowright" size="15" color="#999"></uni-icons>
				</view>
			</view>

			<!-- 用户协议 -->
			<view class="setting-item" @click="openUserAgreement">
				<view class="item-label">用户协议</view>
				<view class="item-value">
					<uni-icons type="arrowright" size="15" color="#999"></uni-icons>
				</view>
			</view>

			<!-- 隐私政策 -->
			<view class="setting-item" @click="openPrivacyPolicy">
				<view class="item-label">隐私政策</view>
				<view class="item-value">
					<uni-icons type="arrowright" size="15" color="#999"></uni-icons>
				</view>
			</view>

			<!-- 去给好评 -->
			<view class="setting-item" @click="goToReview">
				<view class="item-label">去给好评</view>
				<view class="item-value">
					<text class="review-link">前往好评</text>
					<uni-icons type="arrowright" size="15" color="#999"></uni-icons>
				</view>
			</view>
		</view>

		<!-- 退出登录按钮 -->
		<view class="logout-section">
			<button class="logout-btn" @click="showLogoutConfirm">退出登录</button>
		</view>




		<!-- 确认退出弹窗 -->
		<view class="confirm-modal" v-if="showLogoutModal">
			<view class="delete-popup">
				<view class="delete-message">正在退出登录，是否确认</view>
				<view class="delete-buttons">
					<view class="cancel-btn" @click="hideLogoutModal">取消</view>
					<view class="confirm-btn" @tap="confirmLogout">确认</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { request } from '@/utils/request';

export default {
	data() {
		return {
			autoSignIn: false, // 地点自动签到开关状态
			showLogoutModal: false, // 退出登录确认弹窗显示状态
			isLoading: false // 加载状态
		};
	},
	mounted() {
		this.fetchAutoSignInStatus(); // 页面加载时获取自动签到状态
	},
	methods: {
		// 返回上一页
		goBack() {
			uni.navigateBack();
		},

		// 检查更新
		checkUpdate() {
			uni.showToast({
				title: '已是最新版本',
				icon: 'none'
			});
		},

		// 获取自动签到状态
		fetchAutoSignInStatus() {
			this.isLoading = true;
			request({
				url: 'Parttimejobs/getAutoSignInStatus',
				method: 'GET',
				data: {
					user_id: uni.getStorageSync('uid')
				}
			}).then(res => {
				if (res.code === 200) {
					// 设置自动签到状态，1为自动，0为手动
					this.autoSignIn = res.data.qiandao === 1;
				} else {
					console.error('获取自动签到状态失败:', res.msg);
				}
			}).catch(err => {
				console.error('获取自动签到状态请求失败:', err);
			}).finally(() => {
				this.isLoading = false;
			});
		},

		// 地点自动签到开关变化
		onAutoSignInChange(e) {
			const newStatus = e.detail.value;
			this.isLoading = true;
			request({
				url: 'Parttimejobs/updateAutoSignInStatus',
				method: 'POST',
				data: {
					user_id: uni.getStorageSync('uid'),
					qiandao: newStatus ? 1 : 0 // 1为自动，0为手动
				}
			}).then(res => {
				if (res.code === 200) {
					this.autoSignIn = newStatus;
				} else {
					uni.showToast({ title: res.msg || '更新自动签到状态失败', icon: 'none' });
				}
			}).catch(err => {
				console.error('更新自动签到状态请求失败:', err);
				uni.showToast({ title: '网络错误，更新失败', icon: 'none' });
			}).finally(() => {
				this.isLoading = false;
			});
		},

		// 打开用户协议
		openUserAgreement() {
			uni.navigateTo({
				url: '/pages/pageMy/Setting/userAgreement/userAgreement'
			});
		},

		// 打开隐私政策
		openPrivacyPolicy() {
			uni.navigateTo({
				url: '/pages/pageMy/Setting/privacyAgreement/privacyAgreement'
			});
		},

		// 前往好评页面
		goToReview() {
			uni.navigateTo({
				url: '/pages/review/review'
			});
		},

		// 打开修改提现密码页面
		openModifyWithdrawPassword() {
			uni.navigateTo({
				url: '/pages/pageMy/Setting/modifyWithdrawPassword/modifyWithdrawPassword'
			});
		},

		// 显示退出登录确认弹窗
		showLogoutConfirm() {
			this.showLogoutModal = true;

		},

		// 隐藏退出登录确认弹窗
		hideLogoutModal() {
			this.showLogoutModal = false;
		},

		// 确认退出登录
		confirmLogout() {
			uni.removeStorageSync('token');
			// 清除实名认证弹窗标记，以便重新登录后再次显示
			uni.removeStorageSync('hasShownAuthPopup');
			// 这里添加退出登录的逻辑
			uni.showToast({
				title: '退出登录成功',
				icon: 'success',
				duration: 1500
			});

			// 模拟退出登录过程
			setTimeout(() => {
				this.hideLogoutModal();
				// 可以在这里跳转到登录页面
				uni.navigateBack()
			}, 1000);
		}
	}
};
</script>

<style lang="scss" scoped>
.settings-page {
	min-height: 100vh;
	background-color: #F2F3F8;
	padding: 20rpx;

	// 头像区域
	.profile-section {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 40rpx 0;

		.avatar-container {
			width: 160rpx;
			height: 160rpx;
			border-radius: 50%;
			background-color: #f5f5f5;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-bottom: 20rpx;

			.avatar-placeholder {
				width: 148rpx;
				height: 148rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 3rpx 13rpx 0rpx rgba(14, 28, 55, 0.2);
				border-radius: 40rpx;
			}
		}

		.username {
			font-size: 32rpx;
			font-weight: bold;
			color: #333;
			margin-bottom: 10rpx;
		}

		.version {
			font-size: 24rpx;
			color: #999;
			margin-bottom: 30rpx;
		}

		.check-update-btn {
			width: 188rpx;
			height: 65rpx;

			line-height: 60rpx;
			background-color: #fff;
			border: 1rpx solid #ddd;
			border-radius: 33rpx;
			font-family: Source Han Sans CN;
			font-weight: 500;
			font-size: 26rpx;
			color: #0E1C37;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}

	// 设置列表
	.settings-list {
		margin-top: 20rpx;
		background: #FFFFFF;
		border-radius: 26rpx;

		.setting-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 30rpx;
			border-bottom: 1rpx solid #f0f0f0;

			.item-label {
				font-size: 28rpx;
				color: #333;
			}

			.item-value {
				display: flex;
				align-items: center;

				.toggle-switch {
					transform: scale(0.8);
				}


				::v-deep .uni-switch-input:checked {
					// 打开状态背景色
					background-color: #007AFF !important;
					border-color: #007AFF !important;
				}

				.iconfont {
					font-size: 32rpx;
					color: #ccc;
				}

				.review-link {
					font-size: 28rpx;
					color: #007aff;
					margin-right: 10rpx;
				}
			}
		}
	}

	// 退出登录区域
	.logout-section {
		padding: 60rpx 0rpx;

		.logout-btn {
			width: 100%;
			height: 88rpx;
			line-height: 88rpx;
			background-color: #FFFFFF;
			border-radius: 26rpx;
			font-size: 32rpx;
			color: #f56c6c;
		}
	}

	// 确认退出弹窗
	.confirm-modal {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 999;

		.delete-popup {
			width: 484rpx;
			height: 250rpx;
			background: #FFFFFF;
			border-radius: 26rpx;

			.delete-message {
				width: 350rpx;
				height: 28rpx;
				text-align: center;
				margin: 30rpx auto;
				font-family: Source Han Sans CN;
				font-weight: 500;
				font-size: 30rpx;
				color: #000000;
				padding-top: 40rpx;
				padding-bottom: 10rpx;

			}

			.delete-buttons {
				display: flex;
				justify-content: space-around;
				margin-top: 50rpx;
				border-top: 1rpx solid #CDCDCD;
				font-family: Source Han Sans CN;
				font-weight: 500;
				font-size: 32rpx;
				color: #2E66F7;


				.cancel-btn {
					flex: 1;
					width: 224px;
					height: 90rpx;
					display: flex;
					justify-content: center;
					align-items: center;

					border-right: 1rpx solid #CDCDCD;
				}

				.confirm-btn {
					flex: 1;
					width: 224rpx;
					height: 90rpx;
					display: flex;
					justify-content: center;
					align-items: center;


				}
			}
		}
	}
}
</style>